package com.zbf;

import lombok.SneakyThrows;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.CompletableFuture;

/**
 * @author: LCG
 * @date: 2023-02-23 21:13:50
 * @description:
 **/
public class fileTest {
    public static void main(String[] args) throws IOException {


        File file=new File("C:\\Users\\lenovo\\Desktop\\message_0.log");
        FileInputStream fileInputStream=new FileInputStream(file);
        BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(fileInputStream,"utf-8"));

        File fileOut=new File("C:\\Users\\lenovo\\Desktop\\message_0_0.txt");
        FileOutputStream fileOutputStream=new FileOutputStream(fileOut);
        BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(fileOutputStream,"utf-8"));

        File fileOut01=new File("C:\\Users\\lenovo\\Desktop\\message_0_0_1.txt");
        FileOutputStream fileOutputStream01=new FileOutputStream(fileOut01);
        BufferedWriter bufferedWriter01=new BufferedWriter(new OutputStreamWriter(fileOutputStream01,"utf-8"));

        String str=null;
        Map<String,Integer> mapp=new HashMap<String,Integer>();

        Map<String,Integer> mapp2=new HashMap<String,Integer>();
        Map<String,List<String>> mappList=new HashMap<String, List<String>>();

        //存放过滤出数据
        Map<String,List<String>> mappList2=new HashMap<String, List<String>>();
        while ((str=bufferedReader.readLine())!=null){
            if(str.contains("合成消费失败id=")){
                String mobile = str.split(",")[5];
                /**手机号**/
                String s123 = mobile.split("=")[1];
                System.out.println(s123);
                if(mapp.containsKey(s123)){
                    mapp.put(s123,mapp.get(s123)+1);
                    List<String>  list= mappList.get(s123);
                    list.add(str);
                    mappList.put(s123,list);
                }else{
                    mapp.put(s123,1);
                    ArrayList<String> strings = new ArrayList<>();
                    strings.add(str);
                    mappList.put(s123,strings);
                }
                bufferedWriter.write(str);
                bufferedWriter.newLine();
            }


            if(str.contains("合成-正常合成藏品-删除锁记录")){
                bufferedWriter01.write(str);
                bufferedWriter01.newLine();
                String recordId = str.split("ComposeActivityRecords\\(id=")[1].split(",")[0];
                if(mapp2.containsKey(recordId)){
                    mapp2.put(recordId,mapp2.get(recordId)+1);
                    List<String>  list2= mappList2.get(recordId);
                    if(list2==null){
                        System.out.println();
                    }
                    list2.add(str);
                    mappList2.put(recordId,list2);
                }else{
                    mapp2.put(recordId,1);
                    ArrayList<String> strings = new ArrayList<>();
                    strings.add(str);
                    mappList2.put(recordId,strings);
                }
            }

        }
        bufferedWriter.flush();


        File fileOut1=new File("C:\\Users\\lenovo\\Desktop\\mobile_0_0.txt");
        FileOutputStream fileOutputStream2=new FileOutputStream(fileOut1);
        BufferedWriter bufferedWriter2=new BufferedWriter(new OutputStreamWriter(fileOutputStream2,"utf-8"));
        for (Map.Entry<String, Integer> stringIntegerEntry : mapp.entrySet()) {
            String yyy = stringIntegerEntry.getKey() + "-" + stringIntegerEntry.getValue();
            System.out.println(yyy);
            bufferedWriter2.write(yyy);
            bufferedWriter2.newLine();
        }
        bufferedWriter2.flush();



        File fileOut3=new File("C:\\Users\\lenovo\\Desktop\\message_XXXXX.txt");
        FileOutputStream fileOutputStream3=new FileOutputStream(fileOut3);
        BufferedWriter bufferedWriter3=new BufferedWriter(new OutputStreamWriter(fileOutputStream3,"utf-8"));

        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //把相同手机号的数据放在一起
        for (Map.Entry<String, List<String>> stringListEntry : mappList.entrySet()) {

            String key = stringListEntry.getKey();
            List<String> strings = stringListEntry.getValue();

            strings.sort(new Comparator<String>() {
                @SneakyThrows
                @Override
                public int compare(String o1, String o2) {
                    String time1 = o1.split(",")[0];
                    String time2 = o2.split(",")[0];
                    long time01 = simpleDateFormat.parse(time1).getTime();
                    long time02 = simpleDateFormat.parse(time2).getTime();

                    if(time01>time02){
                        return 1;
                    }else if(time01==time02){
                        return 0;
                    }else{
                        return -1;
                    }
                }
            });

            for (String string : strings) {
                bufferedWriter3.write(string);
                bufferedWriter3.newLine();
            }

        }

        bufferedWriter3.flush();



        //筛选出错误的数据
        File fileOut4=new File("C:\\Users\\lenovo\\Desktop\\message_问题藏品数据.txt");
        FileOutputStream fileOutputStream4=new FileOutputStream(fileOut4);
        BufferedWriter bufferedWriter4=new BufferedWriter(new OutputStreamWriter(fileOutputStream4));


        File fileOut5=new File("C:\\Users\\lenovo\\Desktop\\message_最早抽取的数据.txt");
        FileOutputStream fileOutputStream5=new FileOutputStream(fileOut5);
        BufferedWriter bufferedWriter5=new BufferedWriter(new OutputStreamWriter(fileOutputStream5));

        File fileOut6=new File("C:\\Users\\lenovo\\Desktop\\message_卡数据.txt");
        FileOutputStream fileOutputStream6=new FileOutputStream(fileOut6);
        BufferedWriter bufferedWriter6=new BufferedWriter(new OutputStreamWriter(fileOutputStream6));

        Map<String,Integer>  maps=new HashMap<>();

        for (Map.Entry<String, List<String>> stringListEntry : mappList2.entrySet()) {

            List<String> strings2 = stringListEntry.getValue();

            strings2.sort(new Comparator<String>() {
                @SneakyThrows
                @Override
                public int compare(String o1, String o2) {
                    String time1 = o1.split(",")[0];
                    String time2 = o2.split(",")[0];
                    long time01 = simpleDateFormat.parse(time1).getTime();
                    long time02 = simpleDateFormat.parse(time2).getTime();

                    if(time01>time02){
                        return 1;
                    }else if(time01==time02){
                        return 0;
                    }else{
                        return -1;
                    }
                }
            });

           if(strings2.size()>=1){
               for (String string : strings2) {
                   bufferedWriter4.write(string);
                   bufferedWriter4.newLine();
               }
           }

            if(strings2.size()>=1){
                String recordId  = strings2.get(0).split("Records\\(id=")[1].split(",")[0];
                String name  = strings2.get(0).split("name=")[1].split(",")[0];
                String cover = strings2.get(0).split("cover=")[1].split(",")[0];
                String memberId = strings2.get(0).split("memberId=")[1].split(",")[0];

                String info = memberId+"***"+recordId + "***" + name + "***" + cover;

                if(maps.containsKey(name)){
                    maps.put(name,maps.get(name)+1);
                }else{
                    maps.put(name,1);
                }
                bufferedWriter5.write(info);
                bufferedWriter5.newLine();
            }

        }



        bufferedWriter4.flush();
        bufferedWriter5.flush();

        for (Map.Entry<String, Integer> stringIntegerEntry : maps.entrySet()) {
            bufferedWriter6.write(stringIntegerEntry.getKey()+"----"+stringIntegerEntry.getValue());
        }

        bufferedWriter6.flush();
        //过滤出需要更改的数据，也就是如果多次抽取的话  取最早的一条数据


    }
}
